package com.ddwl.goods.service;

import cn.hutool.core.util.StrUtil;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.goods.dao.AcademyColumnDao;
import com.ddwl.goods.dao.ColumnEssayDao;
import com.ddwl.goods.dao.model.Academy;
import com.ddwl.goods.model.AcademyColumn;
import com.ddwl.goods.model.ColumnEssay;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.goods.AcademyColumnEssayVo;
import com.ddwl.schema.vo.goods.AcademyColumnVo;
import com.ddwl.schema.vo.goods.AcademyVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hemiaojun
 * @Date 2020/11/19 16:36
 */
@Service
public class AcademyColumnService {

    @Resource
    private AcademyColumnDao academyColumnDao;
    @Resource
    private ColumnEssayDao columnEssayDao;

    public void add(AcademyColumnBo bo, String userId) {
        AcademyColumn exist = getByName(bo.getName());
        ParamException.isNotNull(exist,"已经存在该名称的栏目");
        AcademyColumn entity = BeanConverUtils.convertBean(bo, AcademyColumn.class);
        entity.setCreatedUserId(userId);
        entity.setModifiedUserId(userId);
        entity.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        academyColumnDao.insert(entity);
    }

    public void update(AcademyColumnUpdateBo bo, String userId) {
        AcademyColumn exist = getByName(bo.getName());
        if (null != exist) {
            ParamException.isFalse((exist.getId().equals(bo.getId())), "已经存在该名称的栏目");
        }
        AcademyColumn entity = BeanConverUtils.convertBean(bo, AcademyColumn.class);
        entity.setModifiedUserId(userId);
        entity.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        academyColumnDao.updateByPrimaryKeySelective(entity);
    }

    private AcademyColumn getByName(String name){
        AcademyColumn entity = new AcademyColumn();
        entity.setName(name);
        entity.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return academyColumnDao.selectOne(entity);
    }

    public AcademyColumnVo get(String id) {
        return BeanConverUtils.convertBean(academyColumnDao.selectByPrimaryKey(id), AcademyColumnVo.class);
    }

    public void delete(String id,String userId) {
        AcademyColumn entity = new AcademyColumn();
        entity.setId(id);
        entity.setModifiedUserId(userId);
        entity.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        academyColumnDao.updateByPrimaryKeySelective(entity);
    }

    public PageVo<AcademyColumnVo> page(PageBo<AcademyColumnBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<AcademyColumn> info = new PageInfo<>(academyColumnDao.getList(pageBo.getParam()));
        List<AcademyColumnVo> list = BeanConverUtils.convertBeanList(info.getList(), AcademyColumnVo.class);
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), list);

    }

    public void effect(AcademyColumnEffectBo bo, String userId) {
        AcademyColumn entity = BeanConverUtils.convertBean(bo, AcademyColumn.class);
        entity.setModifiedUserId(userId);
        academyColumnDao.updateByPrimaryKeySelective(entity);
    }

    public void addEssay(AcademyColumnAddEssayBo bo) {
        deleteColumnEssayByEssayId(bo.getEssayId());
        List<String> list = bo.getAcademyColumnId();
        List<ColumnEssay> columnEssays = new ArrayList<>();
        for (String columnId : list) {
            ColumnEssay entity = new ColumnEssay();
            entity.setId(UUIDGenerator.getShortUUID());
            entity.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
            entity.setGmtCreated(Instant.now());
            entity.setGmtModified(Instant.now());
            entity.setAcademyColumnId(columnId);
            entity.setEssayId(bo.getEssayId());
            columnEssays.add(entity);
        }
        if (!CollectionUtils.isEmpty(columnEssays)) {
            columnEssayDao.batchInsert(columnEssays);
        }
    }

    private void deleteColumnEssayByEssayId(String essayId) {
        columnEssayDao.deleteColumnEssayByEssayId(essayId);
    }

    private ColumnEssay getByColumnIdAndEssayId(String columnId,String essayId){
        ColumnEssay entity = new ColumnEssay();
        entity.setAcademyColumnId(columnId);
        entity.setEssayId(essayId);
        return columnEssayDao.selectOne(entity);
    }

    public List<AcademyVo> getEssayByColumnId(String columnId) {
        List<Academy> list=columnEssayDao.getEssayByColumnId(columnId);
        return BeanConverUtils.convertBeanList(list, AcademyVo.class);
    }

    public void deleteEssay(AcademyColumnDeleteEssayBo bo) {
        ColumnEssay exist = getByColumnIdAndEssayId(bo.getAcademyColumnId(), bo.getEssayId());
        ParamException.isNull(exist,"文章不存在");
        ColumnEssay entity = new ColumnEssay();
        entity.setId(exist.getId());
        entity.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        columnEssayDao.updateByPrimaryKeySelective(entity);
    }

    public List<AcademyColumnVo> getList() {
        AcademyColumn entity = new AcademyColumn();
        entity.setEffect(BaseEnum.StatusEnum.ENABLE.getCode());
        return BeanConverUtils.convertBeanList(academyColumnDao.select(entity),AcademyColumnVo.class);
    }

    public List<AcademyColumnEssayVo> getAcademyColumnEssay() {
        return columnEssayDao.getAcademyColumnEssay();
    }

    public List<String> getColumnEssayByEssayId(String essayId) {
        ColumnEssay entity = new ColumnEssay();
        entity.setEssayId(essayId);
        List<ColumnEssay> list=columnEssayDao.select(entity);
        return list.stream().map(ColumnEssay::getAcademyColumnId).collect(Collectors.toList());
    }

    public PageVo<AcademyVo> pageColumnEssay(PageBo<ColumnEssayPageBo> pageBo) {
        ParamException.isNull(pageBo.getParam(), "栏目不能为空");
        ParamException.isTrue(StrUtil.isEmpty(pageBo.getParam().getAcademyColumnId()), "栏目不能为空");
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<AcademyVo> info=new PageInfo<>(columnEssayDao.getColumnEssayList(pageBo.getParam()));
        return new PageVo<>(info.getTotal(), pageBo.getPage(), pageBo.getPageSize(), info.getList());
    }

    public void essaySort(ColumnEssaySortBo bo) {
        ColumnEssay columnEssay = BeanConverUtils.convertBean(bo, ColumnEssay.class);
        columnEssayDao.essaySort(columnEssay);
    }
}
